package com.dayou.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dayou.bean.InterviewerRecord;
import com.dayou.bean.InterviewerRecordDtl;
import com.dayou.bean.SysDept;
import com.dayou.bean.SysUser;
import com.dayou.common.core.controller.BaseController;
import com.dayou.common.core.domain.AjaxResult;
import com.dayou.common.core.page.TableDataInfo;
import com.dayou.common.exception.BusinessException;
import com.dayou.service.IInterviewService;
import com.dayou.service.ISysUserService;
import com.dayou.service.impl.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/interview")
public class InterviewerController extends BaseController {
    private String prefix = "interview";
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysDeptService deptService;
    @Autowired
    private IInterviewService interviewService;
    @GetMapping()
    public String interview(){
        return prefix+"/login";
    }
    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(@RequestBody SysUser sysUser){
        SysUser loginUser = sysUserService.login(sysUser);
        Integer deptId = loginUser.getDeptId();
        SysDept dept = deptService.selectSysDeptById(deptId);
        switch (dept.getName()) {
            case "管理员":
                throw new BusinessException("管理员账号需通过管理员入口登录");
            case "自助机":
                return AjaxResult.success("登录成功","selfHelp");
            case "讯问室":
                return AjaxResult.success("登录成功","arraign");
            case "等候屏":
                return AjaxResult.success("登录成功","room");
            case "值班室":
                return AjaxResult.success("登录成功","duty");
            case "信息":
                return AjaxResult.success("登录成功","info");
            case "内部人员":
                return AjaxResult.success("登录成功","inner");
            default:
                throw new BusinessException("登录异常");
        }
    }

    @PostMapping("/listInterviewerRecord")
    @ResponseBody
    public AjaxResult listInterviewerRecord(@RequestBody InterviewerRecord record) throws IOException {
        return AjaxResult.success(interviewService.list(record));
    }

    @PostMapping("/insertInterviewerRecord")
    @ResponseBody
    public AjaxResult insertInterviewerRecord(@RequestBody InterviewerRecord record){
        return toAjax(interviewService.insertInterviewerRecord(record));
    }

    @PostMapping("/beginInterviewer")
    @ResponseBody
    public AjaxResult beginInterviewer(@RequestBody InterviewerRecordDtl record){
        record.setState(1);
        record.setBeginTime(new Date());
        return toAjax(interviewService.updateInterviewerRecord(record));
    }

    @PostMapping("/endInterviewer")
    @ResponseBody
    public AjaxResult endInterviewer(@RequestBody InterviewerRecordDtl record){
        record.setState(2);
        record.setEndTime(new Date());
        return toAjax(interviewService.updateInterviewerRecord(record));
    }

    @PostMapping("/delInterviewerRecord")
    @ResponseBody
    public AjaxResult delInterviewerRecord(@RequestBody InterviewerRecord record){
        return toAjax(interviewService.delInterviewerRecord(record.getId()));
    }

    @PostMapping("/selfHelpDel")
    @ResponseBody
    public AjaxResult selfHelpDel(@RequestBody InterviewerRecord record){
        return toAjax(interviewService.selfHelpDel(record));
    }

    @PostMapping("/getInfosBuIrIds")
    @ResponseBody
    public AjaxResult getInfosBuIrIds(@RequestParam String ids){
        return AjaxResult.success(interviewService.getInfosBuIrIds(ids));
    }


    @PostMapping("/getInnerInfo")
    @ResponseBody
    public TableDataInfo getInnerInfo(@RequestBody InterviewerRecord record){
        startPage();
        List<Map<String,Object>> list = interviewService.interviewerMainRecordList(record);
        return getDataTable(list);
    }

    @PostMapping("/getInnerMeetingRecord")
    @ResponseBody
    public AjaxResult getInnerMeetingRecord(@RequestBody InterviewerRecordDtl recordDtl){
        return AjaxResult.success(interviewService.interviewerDetisRecord(recordDtl));
    }

    @PostMapping("/lawyerEntry")
    @ResponseBody
    public AjaxResult lawyerEntry(@RequestBody String body){
        JSONObject jo = (JSONObject)JSON.parse(body);
        String ip = jo.getString("ip");
        Integer command = jo.getInteger("command");
        return AjaxResult.success(interviewService.lawyerEntry(ip,command));
    }

    @GetMapping("comparisonBringRecords")
    @ResponseBody
    public AjaxResult comparisonBringRecords(@RequestParam(value = "areaNo",required = false) Integer areaNo){
       return AjaxResult.success(interviewService.selectBringRecordListByAreaNo(areaNo));
    }
}
